import { defineStore } from 'pinia';

export const businessMethods =  defineStore('businessMethods', {
    state:()=>( {
        bizMethods:[],
        processDefinitionId:''
    }),
    getters: {
        getProcessDefinitionId(): string {
          return this.processDefinitionId;
        },       
        getAllMethods(state){
            return function(){           
            return state.bizMethods;
           }
        },    
        getBusinessMethodById(state) {    
            return function(bizMethodId){                        
            const index = state.bizMethods.findIndex(item => item.id === bizMethodId);  
            console.log(index);
            if (index !== -1) {                
              return {hasValue:true,bizMethod:state.bizMethods[index]}
            }
            else 
              return  {hasValue:false,bizMethod:null}
            }
        },
      },
    actions:{
        setStateData(value){
            this.bizMethods=value;
        },
        setProcessDefinitionId(processDefinitionId:string)
        {
            this.processDefinitionId = processDefinitionId;
        },             
        addBusinessMethod(businessMethod){
            this.bizMethods.push(businessMethod);
            //console.log(this.bizMethods);  
        },
        updateBusinessMethodById(bizMethodId,updatedProperties){            
            const index = this.bizMethods.findIndex(item => item.id === bizMethodId);  
            if (index !== -1) {  
                Object.assign(this.bizMethods[index], updatedProperties);  
            }
            
           
           // this.removeBusinessMethod(bizMethodId);
            //this.addBusinessMethod(businessMethod);
        },        
        removeBusinessMethod(bizMethodId){
            const index = this.bizMethods.findIndex(item => item.id === bizMethodId);  
            if (index !== -1) {  
                this.bizMethods.splice(index,1);  
            }
        }
    }
});

 